<?php
/**
 * Coffee Framework

 * @category   CF
 * @package    CF_db
 * @author     그네 <Jung Sik, Park> <ccooffeee at hotmail dot com>
 * @copyright  Copyright (c) 2009 Jung Sik, Park <ccooffeee at hotmail dot com>
 * @license    doc/LICENSE    Based New BSD License
 * @version    $Id: db.php,v 0.1.3 2010/08/09 ccooffeee Exp $

 * LICENSE

 * 본 프로그램은 New BSD License 를 기본으로 하고 있지만 약간 다른 제약조건을 가지고 있습니다. 
 * 함께 제공된 license file 인  doc/LICENSE 를 꼭 확인하시길 바랍니다.

 * This Source is subject to the New BSD License with SOME CONSTRAINT that is boudled
 * with this package in the file doc/LICENSE 
 */


require_once __DIR__.'/db/interface.php';


class CF_db implements CF_db_interface
{
	private $db;


	public function __construct ($config)
	{
		if($config instanceof CF_config_iterator) 
			$config = $config->to_array();

		if(!is_array($config) || !isset($config['driver'])) {
			require_once __DIR__.'/db/exception.php';
			throw new CF_db_exception('Invalid Config');
        }

		switch($config['driver']) {
			case 'freetds':
			case 'sybase':
			case 'mssql':
			case 'dblib':	$driver = 'mssql';	break;
			case 'sqlsrv':	$driver = 'sqlsrv'; break;
			case 'mysql':	$driver = 'mysql';	break;
			case 'oracle':	$driver = 'oracle';	break;
			case 'pgsql':	$driver = 'pgsql';	break;
			case 'sqlite':	$driver = 'sqlite';	break;
			default:
				require_once __DIR__.'/db/exception.php';
				throw new CF_db_exception($config['driver'].': Not Support this Driver');
			break;
		}

		$class = 'CF_db_'.$driver;

		require_once __DIR__.'/db/'.$driver.'.php';

		$db = new $class ($config);

		$this->_check($db);

		$this->db = $db;
	}

	public function isConnect() {
		$this->_check();
		return $this->db->isConnect();
	}

	public function close() {
		$this->_check();
		$this->db->close();
	}

	public function begin() {
		$this->_check();
		$this->db->begin();
	}

	public function commit() {
		$this->_check();
		$this->db->commit();
	}

	public function rollback($savepoint = null) {
		$this->_check();
		$this->db->rollback($savepoint);
	}

	public function query($sql, array $param = array()) {
		$this->_check();
		$this->db->query($sql, $param);
		return $this;
	}

	public function fetch($mode = null) {
		$this->_check();
		return $this->db->fetch($mode);
	}

	public function fetchAll($mode = null) {
		$this->_check();
		return $this->db->fetchAll($mode);
	}

	public function field($index = 0) {
		$this->_check();
		return $this->db->field($index);
	}

	public function insertId($seq = null) {
		$this->_check();
		return $this->db->insertId($seq);
	}

	public function rowCount() {
		$this->_check();
		return $this->db->rowCount();
	}

	public function free() {
		$this->_check();
		$this->db->free();
	}

	public function escape($string) {
		$this->_check();
		return $this->db->escape($string);
	}

	public function setSavepoint($savepoint = null) {
		$this->_check();
		$this->db->setSavepoint($savepoint);
	}

	public function lowerField() {
		$this->_check();
		$this->db->lowerField();
	}

	public function readLob($lob, $free = true) {
		$this->_check();
		return $this->db->readLob($lob, $free);
	}

	public function preparedStatement ($name = null) {
		$this->_check();
		return $this->db->preparedStatement($name);
	}

	public function __call($method, array $params = array()) {
		require_once __DIR__.'/db/exception.php';
		throw new CF_db_exception('Call to undefined method: '.__CLASS__.'::'.$method.'()');
	}

	private function _check($db = null) {
		$db = $db !== null ? $db : $this->db;

		if($db === null) {
			require_once __DIR__.'/db/exception.php';
			throw new CF_db_exception("Database instance is not created");
		}
		if(!($db instanceof CF_db_interface)) {
			require_once __DIR__.'/db/exception.php';
			throw new CF_db_exception("Class not Implements CF_db_interface");
		}
	}
}

// vim: ts=4 sw=4 fdm=marker
?>
